package com.badshun.skill;

import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.*;
import com.badlogic.gdx.math.*;
import com.badshun.box2d.*;
import com.badlogic.gdx.physics.box2d.*;
import com.badlogic.gdx.utils.*;
import com.badshun.player.*;

public class Skill {
	public Animation skillAnimation;
	
	private float time;
	
	private MainPlayer player;
	private Box2DWorld world;
	private OrthographicCamera camera;
	
	private SpriteBatch batch;
	
	public Body skillBody;
	
	private String name;
	
	public boolean stop;
	
	public Skill(String name,int maxCount,MainPlayer player,Box2DWorld world,OrthographicCamera camera) {
		this.name = name;
		this.player = player;
		this.world = world;
		this.camera = camera;
		
		batch = new SpriteBatch();
		
		defineSkill(name,maxCount);
	}
	
	private void defineSkill(String name,int maxCount) {
		TextureRegion[] skillRegionList = new TextureRegion[maxCount];
		
		for(int i = 0; i < skillRegionList.length; i++) {
			skillRegionList[i] = new TextureRegion(new Texture(Gdx.files.internal("skill/" + name + "/" + (i + 1) + ".png")));
		}
		
		skillAnimation = new Animation(0.08f,skillRegionList);
		skillAnimation.setPlayMode(Animation.PlayMode.NORMAL);
		
		BodyDef bdef = new BodyDef();
		bdef.type = BodyDef.BodyType.StaticBody;
		
		
		if(player.direction == MainPlayer.DIRECTION.UP) {
			bdef.position.set(player.playerBody.getPosition().x,player.playerBody.getPosition().y + skillRegionList[0].getRegionHeight() / 2 + player.getKeyFrame(0).getRegionHeight() / 2);
		} else if(player.direction == MainPlayer.DIRECTION.DOWN) {
			bdef.position.set(player.playerBody.getPosition().x,player.playerBody.getPosition().y - skillRegionList[0].getRegionHeight() / 2 - player.getKeyFrame(0).getRegionHeight() / 2);
		} else if(player.direction == MainPlayer.DIRECTION.LEFT) {
			bdef.position.set(player.playerBody.getPosition().x - skillRegionList[0].getRegionWidth() + player.getKeyFrame(0).getRegionWidth(),player.playerBody.getPosition().y + skillRegionList[0].getRegionHeight() / 2 - player.getKeyFrame(0).getRegionHeight() / 2);
		} else if(player.direction == MainPlayer.DIRECTION.RIGHT) {
			bdef.position.set(player.playerBody.getPosition().x + skillRegionList[0].getRegionWidth() - player.getKeyFrame(0).getRegionWidth(),player.playerBody.getPosition().y + skillRegionList[0].getRegionHeight() / 2 - player.getKeyFrame(0).getRegionHeight() / 2);
		}
		
		skillBody = world.world.createBody(bdef);
		skillBody.setUserData(name);
		
		PolygonShape shape = new PolygonShape();
		shape.setAsBox(skillRegionList[0].getRegionWidth() / 2,skillRegionList[0].getRegionHeight() / 2);
		
		FixtureDef fdef = new FixtureDef();
		fdef.shape = shape;
		fdef.isSensor = true;
		fdef.filter.categoryBits = 32;
		fdef.filter.maskBits = 16;
		
		skillBody.createFixture(fdef).setUserData(name + "SKILL");
	}
	
	public void play() {
		batch.setProjectionMatrix(camera.combined);
		batch.begin();
		batch.draw(skillAnimation.getKeyFrame(time),skillBody.getPosition().x - skillAnimation.getKeyFrames()[0].getRegionWidth() / 2,skillBody.getPosition().y - skillAnimation.getKeyFrames()[0].getRegionHeight() / 2);
		batch.end();
		
		time += Gdx.graphics.getDeltaTime();
		
		stop = skillAnimation.isAnimationFinished(time);
	}
	
	public void destroy() {
		Array<Body> bodyList = new Array<Body>();
		
		world.world.getBodies(bodyList);
		
		for(int i = 0; i < bodyList.size; i++) {
			if(bodyList.get(i).getUserData() != null) {
				if(bodyList.get(i).getUserData().equals(name)) {
					Array<Fixture> fixtureList = bodyList.get(i).getFixtureList();

					for(int j = 0; j < fixtureList.size; j++) {
						if(fixtureList.get(j).getUserData().toString().startsWith(name)) {
							bodyList.get(i).destroyFixture(fixtureList.get(j));
						}
					}

					world.world.destroyBody(bodyList.get(i));
				}
			}
		}
	}
}
